﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.huigo.Framework.Passport;
using com.loto.Common;

namespace com.huigo.B2B.Web
{
    public partial class Login : System.Web.UI.Page
    {
        public string loginName
        {
            get
            {
                return RequestString("loginname");
            }
        }
        public string loginPwd
        {
            get
            {
                return RequestString("loginpwd");
            }
        }
        public string isRember
        {
            get
            {
                return RequestString("isRemember");
            }
        }
        public string toUrl
        {
            get
            {
                return RequestString("toUrl");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CustPassport.ChkCookieLogin())
            {
                Response.Redirect("/center/order/OrderList.aspx");
            }
            else
            {
                if (this.RequestBool("isLogin"))
                {
                    if (string.IsNullOrEmpty(loginName))
                    {
                        MessageBox.Show(this.Page, "请输入用户名");
                        return;
                    }
                    else if (string.IsNullOrEmpty(loginPwd))
                    {
                        MessageBox.Show(this.Page, "请输入密码");
                        return;
                    }
                    else
                    {
                        if (isRember == "true")
                        {
                            CustPassport.Login(loginName, loginPwd, 360);
                        }
                        else
                        {
                            CustPassport.Login(loginName, loginPwd, 0);
                        }
                        if (CustPassport.GetCookieUserId() > 0)
                        {
                            if (!string.IsNullOrEmpty(toUrl))
                            {
                                Response.Redirect(toUrl);
                            }
                            else
                            {
                                Response.Redirect("/center/order/OrderList.aspx");
                            }
                        }
                        else
                        {
                            MessageBox.Show(this.Page, "用户名或密码错误");
                            return;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 请求一个字符串，返回null时返回空字符串
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string RequestString(string name)
        {
            string v = Request[name];
            if (v == null)
            {
                v = string.Empty;
            }
            v = v.Replace("<", "&lt;");
            v = v.Replace(">", "&gt;");
            return v.Trim();
        }
        /// <summary>
        /// Request一个布尔值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool RequestBool(string name)
        {
            string v = Request[name];
            if (v == null)
            {
                return false;
            }
            else
            {
                try
                {
                    return bool.Parse(v);
                }
                catch
                {
                    return false;
                }
            }
        }
    }
}
